package com.deyuanyun.pic.web.support.shiro;

import com.deyuanyun.pic.common.cache.TempStoreDataSupport;
import com.deyuanyun.pic.common.spring.SpringAppliction;
import com.deyuanyun.pic.domain.prvlg.Menu;
import com.deyuanyun.pic.service.prvlg.MenuService;
import com.google.common.collect.ArrayListMultimap;
import org.apache.commons.lang.StringUtils;

import java.util.List;

/**
 * Created by xu on 2015/12/22.
 * 处理shiro权限相关
 */
public class ShiroUrlUtils {
    public static String getPerString(){
        //将菜单查出来放入缓存
        MenuService menuService= SpringAppliction.getBean(MenuService.class);
        List<Menu> menuList = menuService.querySameLevelMenu("","");
        ArrayListMultimap menuListMultimap= ArrayListMultimap.create();
        for (Menu menu : menuList){
            if (StringUtils.isBlank(menu.getParentId())){
                menu.setParentId("0");
            }
            menuListMultimap.put(menu.getParentId(),menu);
        }
        TempStoreDataSupport.pushDataf("application.menus",menuList);

        StringBuilder stringBuilder=new StringBuilder();
        stringBuilder.append("/index.html = anon\n");
        stringBuilder.append("/login.json = anon\n");
        stringBuilder.append("/noPerError.jsp = anon\n");
        stringBuilder.append("/error.jsp = anon\n");
        stringBuilder.append("/image/** = anon\n");
        stringBuilder.append("/app/** = anon\n");
        stringBuilder.append("/loginout.html = logout\n");
        stringBuilder.append("/upload/** = authc\n");
        stringBuilder.append("/menu/** = authc\n");
        stringBuilder.append("/comm/** = authc\n");
        stringBuilder.append("/public/** = authc\n");
        stringBuilder.append("/userSelfInfo/** = authc\n");
        stringBuilder.append("/settings/org/company/search.json = authc\n");
        stringBuilder.append("/pipe/pipeIndex.html = authc,perms[pipeuser]\n");
        stringBuilder.append("/monitoring = authc,perms[admin:manage]\n");
        for (Menu menu:menuList){
           // if (menu.getSort()==null){continue;}
            if (StringUtils.isBlank(menu.getUrl())){continue;}
            String url=StringUtils.substringBeforeLast(menu.getUrl(),"/");
            stringBuilder.append(url+"/** = authc,sperm[\"").append(menu.getCode()).append("\",\"admin:manage").append("\"]\n");
            //int i_ = StringUtils.countMatches(url,"/");
            /*if (i_ > 2) {
                url=StringUtils.substringBeforeLast(menu.getUrl(),"/");
                stringBuilder.append(url+"*//** = authc,sperm[\"").append(menu.getCode()).append("\",\"admin:manage").append("\"]\n");
            }else {
                url=StringUtils.substringBeforeLast(menu.getUrl(),"/");
                //stringBuilder.append(url+" = authc,sperm[\"").append(menu.getCode()).append("\",\"admin:manage").append("\"]\n");
                stringBuilder.append(url+"*//* = authc,sperm[\"").append(menu.getCode()).append("\",\"admin:manage").append("\"]\n");
            }*/

            //stringBuilder.append(url+"/** = authc,perms[\"").append(menu.getCode()).append(",admin:manage").append("\"]\n");

        }
        stringBuilder.append("/** = authc,sperm[admin:manage]\n");
        return stringBuilder.toString();
    }


}
